//
//  ViewController.m
//  05_UIScrollView
//
//  Created by GivenBmli on 16/7/20.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
// 宏定义两种命名方式
// 宏嵌套
// 宏就系统编译时,代码的替换(预编译指令)
#define kScreenBounds  [UIScreen mainScreen].bounds
//一.全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
//二.k + 驼峰命名
#define kScreenWidth kScreenBounds.size.width
#define kScreenHeight kScreenBounds.size.height
@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:kScreenBounds];
    scrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:scrollView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, kScreenWidth - 20, 100);
    button.backgroundColor = [UIColor redColor];
    [scrollView addSubview:button];
//    1.contentSize 默认CGSizeZero CGSizeMake(0,0) 设置可滚动范围
    scrollView.contentSize = CGSizeMake(0, 1000);
//    2.contentOffset 默认CGPointZero 偏移量 滚动位置
//    不设置contentSize,contentOffset 也可以改变
//    问题:改变contentOffset什么变了?
//    答案:改变contentOffset 实质上是对scrollView的bounds进行修改
    NSLog(@"前scrollView fram is:%@",NSStringFromCGRect(scrollView.frame));
    NSLog(@"前scrollView bounds is:%@",NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(0, 500);
    NSLog(@"后scrollView fram is:%@",NSStringFromCGRect(scrollView.frame));
    NSLog(@"后scrollView bounds is:%@",NSStringFromCGRect(scrollView.bounds));

//    3.contentInset 加边距
//    添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
//    4.directionalLockEnabled
//    锁定垂直或水平滚动(45°角锁会失效)
    scrollView.directionalLockEnabled = YES;
//    5.bounces 回弹效果 边缘弹动动画关闭NO
    scrollView.bounces = NO;
//    6.alwaysBounceVertical 垂直方向的回弹效果
//    在没有垂直方向的滚动范围时,保证垂直方向的回弹的效果存在
    scrollView.alwaysBounceVertical = YES;
//    7.alwaysBounceHorizontal 水平滚动范围
//    在没有水平方向的滚动范围时,保证水平方向的回弹的效果存在
    scrollView.alwaysBounceHorizontal = YES;
//    8.pagingEnabled 按页滚动
    scrollView.pagingEnabled = YES;
//    9.scrollEnabled 滚动视图能否滚动, 默认是YES(可以滚动), NO(不能滚动)
    scrollView.scrollEnabled = YES;
//    10. 是否显示垂直/水平 的滚动指示器 默认YES
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = YES;
//    11.滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(10,0,0,0);
//    12.滚动指示器的颜色(深色背景, 浅色背景)
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
//    13.相关用户操作
//    tracking:用户是否开始拖动
//    scrollView.tracking
//    dragging:用户是否正在拖动
//    scrollView.dragging
//    decelerating:是否减速中
//    scrollView.decelerating
//    14. 滚动视图,双击状态栏手势是否存在 默认YES(单视图有效)
//    scrollView.scrollsToTop = YES;
//    15.代理
    scrollView.delegate = self;
//    16.键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

    UITextField *textFild = [[UITextField alloc]initWithFrame:CGRectMake(10, 300, kScreenWidth -20 , 100)];
    textFild.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:textFild];
//    17. 缩放系数
//    如果想进行缩放, 必须实现 viewForZoomingInScrollView
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
    
}
// 与其想要缩放的子视图绑定,scrollView的contentSize
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}

// 滚动视图滚动就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    tracking 用户触摸
    if (scrollView.tracking) {
        NSLog(@"拖拽滚动");
    }
//    dragging 用户开始滑动
    if (scrollView.dragging) {
        NSLog(@"用户开始滑动");
    }
//    decelerating 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"用户触摸结束");
    }

}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//    NSLog(@"开始拖拽");

}
// 结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    NSLog(@"结束拖拽");
}
// 开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//    NSLog(@"开始减速");
}
//结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    NSLog(@"结束减速");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
